﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using System.Web.Script.Serialization;
using CommonUtility;
using BLL;

namespace BLL
{
    public class bllUserRole
    {
        /// <summary>
        ///User界面维护Role下拉框数据
        /// </summary>
        /// <returns></returns>
        public static string GetUserRoleList(UserInfo _operator)
        {
            string result = "";
            try
            {
                IList<RoleInfo> list = new List<RoleInfo>();
                list = Role.GetList("[Flag]='A'");
                foreach (RoleInfo clsF in list)
                {
                    clsF.RoleDesc = clsF.RoleID + " - " + clsF.RoleDesc;
                }
                result = new JavaScriptSerializer().Serialize(list);
            }
            catch (Exception ex)
            {
                result = "ERR";
                Common.Application_Error_Server("BLL/bllUserRole.cs/GetUserRoleList() " + _operator.UserID, ex);
            }

            return result;
        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="_operator"></param>
        /// <returns></returns>
        public static string GetUserRole(UserInfo _operator)
        {
            string result = "";
            try
            {
                IList<UserRoleInfo> list = new List<UserRoleInfo>();
                list = UserRole.GetList("[Flag]='A' and userID='" + _operator.UserID + "'");
                result = new JavaScriptSerializer().Serialize(list);
            }
            catch (Exception ex)
            {
                result = "ERR";
                Common.Application_Error_Server("BLL/bllUserRole.cs/GetUserRole() " + _operator.Name, ex);
            }

            return result;
        }

        public static void DeleleByUserID(string _userid, System.Data.SqlClient.SqlTransaction _myTran)
        {
            IList<UserRoleInfo> list = UserRole.GetList("[UserID]='" + _userid + "'");
            foreach (var user in list)
                UserRole.Delete(user, _myTran);
        }

        public static void AddUserRole(string _userid, string _roleid, string _operaterid, System.Data.SqlClient.SqlTransaction _myTran)
        {
            UserRoleInfo ur = new UserRoleInfo();
            ur.RoleID = _roleid;
            ur.Flag = "A";
            ur.UserID = _userid;
            ur.Whom = _operaterid;
            UserRole.Add(ur, _myTran);
        }

        /// <summary>
        /// 设置用户角色
        /// </summary>
        /// <param name="_funID">页面功能ID</param>
        /// <param name="_userID">UserID</param>
        /// <param name="_userID">RoleList</param>
        /// <returns></returns>
        public static string SetRole(string _funID, string _userID, string _roleList, UserInfo _operator)
        {
            string result = string.Empty;
            System.Data.SqlClient.SqlTransaction myTran = SQLCommon.GetSqlTransaction();
            System.Data.SqlClient.SqlConnection conn = myTran.Connection;
            try
            {
                UserRole UsRl = new UserRole();
                //更新用户角色关系
                if (!string.IsNullOrEmpty(_userID))
                    DeleleByUserID(_userID, myTran);

                if (!string.IsNullOrEmpty(_roleList))
                {
                    string[] arrayRLID = _roleList.Split('/');
                    for (int j = 0; j < arrayRLID.Length; j++)
                    {
                        if (!string.IsNullOrEmpty(arrayRLID[j]))
                            AddUserRole(_userID, arrayRLID[j], _operator.UserID, myTran);
                    }
                }
                bllLog.AddLog(_funID, "Set Role", _userID + "--" + _roleList, _operator.UserID, myTran);

                myTran.Commit();

            }
            catch (Exception ex)
            {
                result = ex.Message;
                myTran.Rollback();
                Common.Application_Error_Server("BLL/bllUserRole.cs/SaveUser() " + _operator.Name, ex);
            }
            finally
            {
                conn.Close();
                conn.Dispose();
                myTran.Dispose();
            }

            return result;
        }

    }
}
